//
//  DetailView.m
//  Daybook
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "DetailView.h"
#import "Common.h"
#import "MJRefresh.h"

#define TEXT_COLOR [UIColor whiteColor]
#define REFRESH_DURATION 1.0

#define REFRESH_COLOR ORIGIN_COLOR

@interface DetailView ()
<
    UITableViewDataSource,
    UITableViewDelegate
>

@property (nonatomic, strong) UITableView *detailTableView;
@property (nonatomic, strong) BaseView *headView;
@property (nonatomic, strong) UICountingLabel *balanceLabel;
@property (nonatomic, strong) UILabel *incomeLabel;
@property (nonatomic, strong) UILabel *outlayLabel;

@property (nonatomic, strong) UILabel *balanceNameLabel;
@property (nonatomic, strong) UILabel *incomeNameLabel;
@property (nonatomic, strong) UILabel *outlayNameLabel;
@property (nonatomic, strong) UIView *upLineView;
@property (nonatomic, strong) UIView *middleLineView;

@property(nonatomic, strong)NSMutableArray *sectionArray;//section标题
@property(nonatomic, strong)NSMutableArray *rowInSectionArray;//section中的cell个数
@property(nonatomic, strong)NSMutableArray *selectedArray;//是否被点击//这个用于判断展开还是缩回当前section的cell
@property (nonatomic, strong) NSMutableArray *weekArray; // 周别的数组

@property (nonatomic, strong) UITapGestureRecognizer *sectionViewTap;

// sectionView
@property (nonatomic, strong) UILabel *sectionWeekendLabel;
@property (nonatomic, strong) UILabel *sectionWeekendNameLabel;
@property (nonatomic, strong) UILabel *sectionDateLabel;

@property (nonatomic, strong) UILabel *sectionBalanceNameLabel;
@property (nonatomic, strong) UILabel *sectionBalanceLabel;
@property (nonatomic, strong) UIView *sectionBalanceLineView;

// 收入支出都有
@property (nonatomic, strong) UILabel *sectionIncomeNameLabel;
@property (nonatomic, strong) UIView *sectionIncomeLineView;
@property (nonatomic, strong) UILabel *sectionIncomeLabel;

@property (nonatomic, strong) UILabel *sectionOutlayNameLabel;
@property (nonatomic, strong) UILabel *sectionOutlayLabel;
@property (nonatomic, strong) UIView *sectionOutlayLineView;

// 单个收入或支出
@property (nonatomic, strong) UILabel *sectionOnlyIncomeNameLabel;
@property (nonatomic, strong) UIView *sectionOnlyIncomeLineView;
@property (nonatomic, strong) UILabel *sectionOnlyIncomeLabel;

@property (nonatomic, strong) UILabel *sectionOnlyOutlayNameLabel;
@property (nonatomic, strong) UILabel *sectionOnlyOutlayLabel;
@property (nonatomic, strong) UIView *sectionOnlyOutlayLineView;

@property (nonatomic, strong) UIView *sectionBottomLineView;

@property (nonatomic , strong) NSDate *date;

@property (nonatomic, strong) MJRefreshBackNormalFooter *footer;
@property (nonatomic, strong) MJRefreshNormalHeader *header;

@property (nonatomic, strong) NSDate *nowDate;

// 收入/支出  计算结果
@property (nonatomic, strong) NSMutableArray *sectionResetArr;

@property (nonatomic, strong) NSMutableArray *detailArr;



@end
@implementation DetailView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.sectionArray = [NSMutableArray array];
        self.weekArray = [NSMutableArray array];
        self.rowInSectionArray = [NSMutableArray array];
        self.selectedArray = [NSMutableArray array];
        self.sectionResetArr = [NSMutableArray array];
        self.detailArr = [NSMutableArray array];
        [self createTableView];
        [self createHeadView];
        [self headerRefresh];
        [self footRefresh];
        [self resetTopData];
    }
    return self;
}

- (void)resetTopData {
//    NSNumber *sumIncome = [accountBooks sumOfProperty:@"income"];
    NSString *incomeStr = [RealmTool incomeStringFromCurrentAccountFromUserDefaults];
    _incomeLabel.text = incomeStr;
    
//    NSNumber *sumMoney = [accountBooks sumOfProperty:@"money"];
    NSString *payStr = [RealmTool payStringFromCurrentAccountFromUserDefaults];
    _outlayLabel.text = payStr;
    
    NSString *str = [NSString stringWithFormat:@"%.2f", incomeStr.doubleValue - payStr.doubleValue];
    _balanceLabel.format = @"%.2f%";
    [_balanceLabel countFrom:0 to:str.floatValue withDuration:1.2];
}



- (void)createData {
    NSInteger count = [[CalendarTool shareCalendarTool] arrayMonthWeekendFromDate:_date].count;
    [_sectionArray removeAllObjects];
    for (NSInteger i = count; i > 0; i--) {
        [_sectionArray addObject:[NSString stringWithFormat:@"%ld", i]];
    }
    _weekArray = [[CalendarTool shareCalendarTool] arrayMonthWeekendFromDate:_date];
    
    [_selectedArray removeAllObjects];
    for (NSInteger i = 0; i < count; i++) {
        [_selectedArray addObject:@"0"];
    }
    [self resetDate];
    [self resetTopData];
}

- (void)resetDate {
    NSLog(@"此时的Date:Hhhh: %@", [[CalendarTool shareCalendarTool] stringFromDate:_date]);

    NSMutableArray *tempArr = [[DetailDealTool shareDetailTool] detailModalArrayFromDataBaseJudgeByDate:_date];
    
    [self.detailArr removeAllObjects];
    for (NSInteger i = tempArr.count - 1; i >= 0 ; i--) {
        NSArray *tempWeekArr = tempArr[i];
        DetailModal *modal = tempWeekArr[0];
        [self.detailArr addObject:modal];
    }
    /******************  cell  *******************/
    
    //每个分区中cell的个数
    for (NSInteger i = 0 ; i < _detailArr.count; i++) {
        DetailModal *modal = _detailArr[i];
        NSString *count = [NSString stringWithFormat:@"%ld", modal.cellArr.count];
        [_rowInSectionArray addObject:count];
    }
}

- (void)createTableView {
    self.detailTableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    [self addSubview:_detailTableView];
    self.detailTableView.backgroundColor = [UIColor whiteColor];
    
    self.detailTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    self.detailTableView.delegate = self;
    self.detailTableView.dataSource = self;
    self.detailTableView.rowHeight = [CalculateFrameView setHeight:60];
    
    self.date = [NSDate date];
    self.nowDate = _date;
    [self createData];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detailViewDataChange) name:@"detailViewDataChange" object:nil];
}

//- (void)detailViewDataChange {
//    [self createData];
//    // 刷新表格
//    [self.detailTableView reloadData];
//    
//    [self resetTopData];
//}

- (void)createHeadView {
    self.headView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, [CalculateFrameView setHeight:130])];
    self.detailTableView.tableHeaderView = _headView;
    _headView.backgroundColor = BGCOLOR;
    
    self.balanceNameLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        tempLabel.center = CGPointMake(MAIN_WIDTH / 2, (MAIN_HEIGHT * (30 / 736.0)));
        [self.headView addSubview:tempLabel];
        tempLabel.font = [UIFont systemFontOfSize:15];
        tempLabel.textAlignment = 1;
        [tempLabel setText:@"结余"];
        tempLabel.textColor = TEXT_COLOR;
        tempLabel;
    });
    
    self.balanceLabel = ({
        UICountingLabel *tempLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(0, 0,  MAIN_WIDTH, 50 / 414.0 * MAIN_WIDTH)];
        tempLabel.center = CGPointMake(MAIN_WIDTH / 2, (MAIN_HEIGHT * (60 / 736.0)));
        [self.headView addSubview:tempLabel];
        tempLabel.font = [UIFont systemFontOfSize:28];
        tempLabel.textAlignment = 1;
        tempLabel.textColor = TEXT_COLOR;
        tempLabel;
    });
    

    
    self.upLineView = [[UIView alloc] initWithFrame:CGRectMake(0, (MAIN_HEIGHT * (90 / 736.0)), MAIN_WIDTH, 1)];
    [self.headView addSubview:_upLineView];
    _upLineView.backgroundColor = LINE_COLOR;
    
    self.middleLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, [CalculateFrameView setHeight:40])];
    self.middleLineView.center = CGPointMake(MAIN_WIDTH / 2, [CalculateFrameView setHeight:110]);

    [self.headView addSubview:_middleLineView];
    _middleLineView.backgroundColor = LINE_COLOR;
    
    self.incomeNameLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:15], [CalculateFrameView setHeight:90], 50 / 414.0 * MAIN_WIDTH, 40 / 414.0 * MAIN_WIDTH)];
        [self.headView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentLeft;
        tempLabel.font = [UIFont systemFontOfSize:15];
        [tempLabel setText:@"收入"];
        tempLabel.textColor = TEXT_COLOR;
        tempLabel;
    });
    
    self.incomeLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:45], [CalculateFrameView setHeight:90], 150 / 414.0 * MAIN_WIDTH, 40 / 414.0 * MAIN_WIDTH)];
        [self.headView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentRight;
        tempLabel.font = [UIFont systemFontOfSize:15];
        tempLabel.textColor = TEXT_COLOR;
        tempLabel;
    });
    
    self.outlayNameLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:218], [CalculateFrameView setHeight:90], 50 / 414.0 * MAIN_WIDTH, 40 / 414.0 * MAIN_WIDTH)];
        [self.headView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentLeft;
        tempLabel.font = [UIFont systemFontOfSize:15];
        [tempLabel setText:@"支出"];
        tempLabel.textColor = TEXT_COLOR;
        tempLabel;
    });
    
    self.outlayLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:253], [CalculateFrameView setHeight:90], 150 / 414.0 * MAIN_WIDTH, 40 / 414.0 * MAIN_WIDTH)];
        [self.headView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentRight;
        tempLabel.font = [UIFont systemFontOfSize:15];
        tempLabel.textColor = TEXT_COLOR;
        tempLabel;
    });
}

#pragma mark cell的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailModal *detailModal = self.detailArr[indexPath.section];
    NSArray *cellArr = detailModal.cellArr;
    NSMutableArray *newCellArr = [NSMutableArray array];
    for (NSInteger i = 0; i < cellArr.count; i++) {
        NSArray *dayArr = cellArr[i];
        for (DetailDayModal *modal in dayArr) {
            [newCellArr addObject:modal];
        }
    }
 
    if (newCellArr.count == 0) {
        static NSString *identifier = @"DetailNoRecordCell";
        DetailNoRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[DetailNoRecordCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        return cell;
    }else {
        static NSString *identifier = @"DetaiCell";
        DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[DetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        DetailDayModal *dayModal = newCellArr[indexPath.row];
        cell.weekendLabel.text = dayModal.week;
        cell.dayLabel.text = dayModal.day;
        cell.picImageView.image = [UIImage imageNamed:dayModal.image];

        NSString *remarksStr = [NSString stringWithFormat:@"%@", [dayModal.cellRemarks class]];
        if ([remarksStr isEqualToString:@"NSTaggedPointerString"]) {
            cell.firstStyleTitleLabel.text = dayModal.cellTitle;
            cell.secondStyleTitleLabel.hidden = YES;
            cell.commentLabel.hidden = YES;
            cell.firstStyleTitleLabel.hidden = NO;
        }else {
            cell.secondStyleTitleLabel.text = dayModal.cellTitle;
            cell.commentLabel.text = dayModal.cellRemarks;
            cell.secondStyleTitleLabel.hidden = NO;
            cell.commentLabel.hidden = NO;
            cell.firstStyleTitleLabel.hidden = YES;
        }
        cell.moneyLabel.text = dayModal.money;
        if ([dayModal.cellType isEqualToString:@"1"]) {
            cell.moneyLabel.textColor = [UIColor redColor];
        }else {
            cell.moneyLabel.textColor = [UIColor greenColor];
        }
        
        //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

#pragma mark cell的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //判断section的标记是否为1,如果是说明为展开,就返回真实个数,如果不是就说明是缩回,返回0.
    if ([_selectedArray[section] isEqualToString:@"1"]) {
        DetailModal *detailModal = self.detailArr[section];
        NSArray *cellArr = detailModal.cellArr;
        NSMutableArray *newCellArr = [NSMutableArray array];
        for (NSInteger i = 0; i < cellArr.count; i++) {
            NSArray *dayArr = cellArr[i];
            for (DetailDayModal *modal in dayArr) {
                [newCellArr addObject:modal];
            }
        }
        if (newCellArr.count == 0) {
            return 1;
        }else {
            return newCellArr.count;
        }
    }
    return 0;
}
#pragma mark section的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sectionArray.count;
}

#pragma cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [CalculateFrameView setHeight:70];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    DetailModal *detailModal = self.detailArr[indexPath.section];
    NSArray *cellArr = detailModal.cellArr;
    NSMutableArray *newCellArr = [NSMutableArray array];
    for (NSInteger i = 0; i < cellArr.count; i++) {
        NSArray *dayArr = cellArr[i];
        for (DetailDayModal *modal in dayArr) {
            [newCellArr addObject:modal];
        }
    }
    DetailDayModal *dayModal = newCellArr[indexPath.row];
    
    RLMResults *currentResult = [RealmTool getCurrentAccountFromUserDefaults];
    DayBook *currentDayBook = [currentResult firstObject];
    
    RLMResults *accountBook = [currentDayBook.bookContent objectsWhere:@"time =  %@", dayModal.time];
    
//    RLMRealm *realm = [RLMRealm defaultRealm];
//    RLMResults<AccountBook *> *accountBook = [AccountBook objectsInRealm:realm where:@"time =  %@", dayModal.time];
    Account *account = accountBook[0];
    RightDayModel *model = [RightDayModel new];
    model.time = dayModal.time;
    model.name = account.name;
    model.type = account.type;
    if ([account.type isEqualToString:@"1"]) {
        model.money = [NSString stringWithFormat:@"%.2f", account.income.doubleValue];
    }else {
        model.money = [NSString stringWithFormat:@"%.2f", account.money.doubleValue];
    }
    model.remarks = account.remarks;
    model.picName = account.picName;
    [self.delegate jumpToIntroduceViewControllerWithModel:model];
}

#pragma mark - section内容
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    DetailModal *detailModal = self.detailArr[section];
    
    self.sectionViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionViewTapAction:)];
    self.sectionViewTap.numberOfTapsRequired = 1;
    
    //每个section上面有一个view,给view一个tag值,用于在点击事件中改变_selectedArray[[tap view].tag - 1000]的值
    UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, [CalculateFrameView setHeight:70])];
    [sectionView addGestureRecognizer:_sectionViewTap];
    sectionView.backgroundColor = LIGHT_GRAY_SECTION;
    
    sectionView.tag = 1000 + section;
    
    /***********************     周      ***********************/

    
    self.sectionWeekendLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:10], [CalculateFrameView setHeight:10], 30 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentCenter;
        tempLabel.font = [UIFont systemFontOfSize:25];
        tempLabel.text = _sectionArray[section];
        tempLabel.textColor = BLACK_COLOR;
        tempLabel;
    });
    
    self.sectionWeekendNameLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:30], [CalculateFrameView setHeight:12], 30 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentCenter;
        tempLabel.font = [UIFont systemFontOfSize:14];
        [tempLabel setText:@"周"];
        tempLabel.textColor = BLACK_COLOR;
        tempLabel;
    });
    
    self.sectionWeekendNameLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:17], [CalculateFrameView setHeight:33], 102 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentLeft;
        tempLabel.font = [UIFont systemFontOfSize:13 * (MAIN_WIDTH / 414.0)];
        tempLabel.text = _weekArray[_weekArray.count - 1 - section];
        tempLabel.textColor = BLACK_COLOR;
        tempLabel;
    });
    
    /***********************     收入       ***********************/

    
    self.sectionIncomeNameLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:100], [CalculateFrameView setHeight:1], 30 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentCenter;
        tempLabel.font = [UIFont systemFontOfSize:14];
        [tempLabel setText:@"收"];
        tempLabel.textColor = [UIColor redColor];
        tempLabel;
    });
    
    self.sectionIncomeLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:130], [CalculateFrameView setHeight:1], 200 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentLeft;
        tempLabel.font = [UIFont systemFontOfSize:15];
        tempLabel.textColor = [UIColor redColor];
        tempLabel;
    });
    
    self.sectionIncomeLineView = [[UIView alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:110], [CalculateFrameView setHeight:29], 200 / 414.0 * MAIN_WIDTH, 1)];
    [sectionView addSubview:_sectionIncomeLineView];
    _sectionIncomeLineView.backgroundColor = [UIColor redColor];
    

    
    /***********************     支出       ***********************/
    
    self.sectionOutlayNameLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:100], [CalculateFrameView setHeight:31], 30 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentCenter;
        tempLabel.font = [UIFont systemFontOfSize:14];
        [tempLabel setText:@"支"];
        tempLabel.textColor = [UIColor greenColor];
        tempLabel;
    });
    
    self.sectionOutlayLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:130], [CalculateFrameView setHeight:31], 200 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentLeft;
        tempLabel.font = [UIFont systemFontOfSize:15];
        tempLabel.textColor = [UIColor greenColor];
        tempLabel;
    });
    
    self.sectionOutlayLineView = [[UIView alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:110], [CalculateFrameView setHeight:59], 50 / 414.0 * MAIN_WIDTH, 1)];
    [sectionView addSubview:_sectionOutlayLineView];
    _sectionOutlayLineView.backgroundColor = [UIColor greenColor];
    

    
    /***********************     余额       ***********************/

#pragma mark - 余额
    self.sectionBalanceNameLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:370], [CalculateFrameView setHeight:1], 30 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentCenter;
        tempLabel.font = [UIFont systemFontOfSize:14];
        [tempLabel setText:@"余"];
        tempLabel.textColor = [UIColor blackColor];
        tempLabel;
    });
    
    self.sectionBalanceLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:192], [CalculateFrameView setHeight:27], 200 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentRight;
        tempLabel.font = [UIFont systemFontOfSize:20];
        [tempLabel setText:@"0.00"];
        tempLabel.textColor = [UIColor blackColor];
        tempLabel;
    });
    
    self.sectionBalanceLineView = [[UIView alloc] initWithFrame:CGRectMake(MAIN_WIDTH - [CalculateFrameView setWidth:2 + 23], [CalculateFrameView setHeight:59], 2 / 414.0 * MAIN_WIDTH, 2)];
    [sectionView addSubview:_sectionBalanceLineView];
    _sectionBalanceLineView.backgroundColor = [UIColor darkGrayColor];
    

#pragma mark - 单个收入或支出
    /***********************     收入      ***********************/
    self.sectionOnlyIncomeNameLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:100], [CalculateFrameView setHeight:12], 30 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentCenter;
        tempLabel.font = [UIFont systemFontOfSize:14];
        [tempLabel setText:@"收"];
        tempLabel.textColor = [UIColor redColor];
        tempLabel;
    });
    
    self.sectionOnlyIncomeLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:130], [CalculateFrameView setHeight:12], 200 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentLeft;
        tempLabel.font = [UIFont systemFontOfSize:15];
        tempLabel.textColor = [UIColor redColor];
        tempLabel;
    });
    
    self.sectionOnlyIncomeLineView = [[UIView alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:110], [CalculateFrameView setHeight:40], 140 / 414.0 * MAIN_WIDTH, 1)];
    [sectionView addSubview:_sectionOnlyIncomeLineView];
    _sectionOnlyIncomeLineView.backgroundColor = [UIColor redColor];
    
    /***********************     支出       ***********************/
    self.sectionOnlyOutlayNameLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:100], [CalculateFrameView setHeight:12], 30 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentCenter;
        tempLabel.font = [UIFont systemFontOfSize:14];
        [tempLabel setText:@"支"];
        tempLabel.textColor = [UIColor greenColor];
        tempLabel;
    });
    
    self.sectionOnlyOutlayLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:130], [CalculateFrameView setHeight:12], 200 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentLeft;
        tempLabel.font = [UIFont systemFontOfSize:15];
        tempLabel.textColor = [UIColor greenColor];
        tempLabel;
    });
    
    self.sectionOnlyOutlayLineView = [[UIView alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:110], [CalculateFrameView setHeight:40], 140 / 414.0 * MAIN_WIDTH, 1)];
    [sectionView addSubview:_sectionOnlyOutlayLineView];
    _sectionOnlyOutlayLineView.backgroundColor = [UIColor greenColor];
    
    self.sectionBottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, [CalculateFrameView setHeight:69], MAIN_WIDTH, 1)];
    [sectionView addSubview:_sectionBottomLineView];
    _sectionBottomLineView.backgroundColor = LIGHT_GRAY;
    
#pragma mark - 收入或支出隐藏
    // firstStyle
    self.sectionIncomeNameLabel.hidden = YES;
    self.sectionIncomeLineView.hidden = YES;
    
    self.sectionOutlayNameLabel.hidden = YES;
    self.sectionOutlayLineView.hidden = YES;
    
    // secondStyle
    _sectionOnlyIncomeNameLabel.hidden = YES;
    _sectionOnlyIncomeLineView.hidden = YES;
    
    _sectionOnlyOutlayLineView.hidden = YES;
    _sectionOnlyOutlayNameLabel.hidden = YES;
    
    
    
#pragma mark - 收入, 支出, 余额赋值
    double maxNum = 0;
    double incomeLength = detailModal.sectionInMoney.doubleValue;
    double outLayLength = detailModal.sectionOutMoney.doubleValue;
    double balanceLength = detailModal.sectionBalance.doubleValue;
    
    maxNum = [self maxOfThreeNumberOne:incomeLength two:outLayLength three:balanceLength];
    
    double fabsIncome = fabs(incomeLength);
    double fabsOutLay = fabs(outLayLength);
    double fabsBalance = fabs(balanceLength);
    
    NSString *tempNumStr = [NSString stringWithFormat:@"%.f", maxNum];
    NSInteger countOfNum = tempNumStr.length - 4;
//    NSLog(@"此时: %ld", countOfNum);
    if (maxNum <= 1000) {
        incomeLength = fabsIncome / maxNum * (countOfNum * 25 + 100);
        outLayLength = fabsOutLay / maxNum * (countOfNum * 25 + 100);
        balanceLength = fabsBalance / maxNum * (countOfNum * 25 + 100);
    }else {
        incomeLength = fabsIncome / maxNum * (countOfNum * 25 + 70);
        outLayLength = fabsOutLay / maxNum * (70 + countOfNum * 25);
        balanceLength = fabsBalance / maxNum * (70 + countOfNum * 25);
    }

//    
//    NSLog(@"income: %.2f", detailModal.sectionInMoney.doubleValue);
//    NSLog(@"outLay: %.2f", detailModal.sectionOutMoney.doubleValue);
//    NSLog(@"balance: %.2f", detailModal.sectionBalance.doubleValue);
    
    // 判断该用哪种Label赋值
    if (outLayLength == 0) {
        // 单个收入
//        NSLog(@"执行单个收入");
        _sectionOnlyIncomeLabel.text = detailModal.sectionInMoney;
        if (isnan(incomeLength) || isinf(incomeLength) || incomeLength == 0) {
            _sectionOnlyIncomeNameLabel.hidden = YES;
            _sectionOnlyIncomeLineView.hidden = YES;
            _sectionOnlyIncomeLabel.hidden = YES;
        }else {
            if (incomeLength < 0.1) {
                _sectionOnlyIncomeLineView.frame = CGRectMake([CalculateFrameView setWidth:110], [CalculateFrameView setHeight:40], 1 / 414.0 * MAIN_WIDTH, 1);
            }else {
                _sectionOnlyIncomeLineView.frame = CGRectMake([CalculateFrameView setWidth:110], [CalculateFrameView setHeight:40], incomeLength / 414.0 * MAIN_WIDTH, 1);
            }
            _sectionOnlyIncomeLineView.hidden = NO;
            _sectionOnlyIncomeNameLabel.hidden = NO;
            _sectionOnlyIncomeLabel.hidden = NO;
        }
        
    }else if (incomeLength == 0){
        // 单个支出
//        NSLog(@"执行单个支出");
        _sectionOnlyOutlayLabel.text = detailModal.sectionOutMoney;
        if (isnan(outLayLength) || isinf(outLayLength) || outLayLength == 0) {
            _sectionOnlyOutlayNameLabel.hidden = YES;
            _sectionOnlyOutlayLineView.hidden = YES;
            _sectionOnlyOutlayLabel.hidden = YES;
        }else {
            if (outLayLength < 0.1) {
                _sectionOnlyOutlayLineView.frame = CGRectMake([CalculateFrameView setWidth:110], [CalculateFrameView setHeight:40], 1 / 414.0 * MAIN_WIDTH, 1);
            }else {
                _sectionOnlyOutlayLineView.frame = CGRectMake([CalculateFrameView setWidth:110], [CalculateFrameView setHeight:40], outLayLength / 414.0 * MAIN_WIDTH, 1);
            }
            _sectionOnlyOutlayNameLabel.hidden = NO;
            _sectionOnlyOutlayLineView.hidden = NO;
            _sectionOnlyOutlayLabel.hidden = NO;
        }
        
    }else {
        // 收入赋值
//        NSLog(@"执行有收入有支出");
        _sectionIncomeLabel.text = detailModal.sectionInMoney;
        if (isnan(incomeLength) || isinf(incomeLength) || incomeLength == 0) {
            _sectionIncomeNameLabel.hidden = YES;
            _sectionIncomeLineView.hidden = YES;
            _sectionIncomeLabel.hidden = YES;
        }else {
            if (incomeLength < 0.1) {
                _sectionIncomeLineView.frame = CGRectMake([CalculateFrameView setWidth:110], [CalculateFrameView setHeight:29], [CalculateFrameView setWidth:1], 1);
            }else {
                _sectionIncomeLineView.frame = CGRectMake([CalculateFrameView setWidth:110], [CalculateFrameView setHeight:29], [CalculateFrameView setWidth:incomeLength], 1);
            }
            _sectionIncomeNameLabel.hidden = NO;
            _sectionIncomeLineView.hidden = NO;
            _sectionIncomeLabel.hidden = NO;
        }

        // 支出赋值
        _sectionOutlayLabel.text = detailModal.sectionOutMoney;
        if (isnan(outLayLength) || isinf(outLayLength) || outLayLength == 0) {
            _sectionOutlayNameLabel.hidden = YES;
            _sectionOutlayLabel.hidden = YES;
            _sectionOutlayLineView.hidden = YES;
        }else {
            if (outLayLength < 0.1) {
                _sectionOutlayLineView.frame = CGRectMake([CalculateFrameView setWidth:110], [CalculateFrameView setHeight:59], 1 / 414.0 * MAIN_WIDTH, 1);
            }else {
                _sectionOutlayLineView.frame = CGRectMake([CalculateFrameView setWidth:110], [CalculateFrameView setHeight:59], outLayLength / 414.0 * MAIN_WIDTH, 1);
            }
            _sectionOutlayNameLabel.hidden = NO;
            _sectionOutlayLabel.hidden = NO;
            _sectionOutlayLineView.hidden = NO;
        }
    }
    
    // 余额赋值
    _sectionBalanceLabel.text = detailModal.sectionBalance;
    if (isnan(balanceLength) || isinf(balanceLength) || balanceLength == 0) {
        _sectionBalanceLabel.text = @"0.00";
    }else {
        if (balanceLength < 0.1) {
            _sectionBalanceLineView.frame = CGRectMake(MAIN_WIDTH - [CalculateFrameView setWidth:2 + 23], [CalculateFrameView setHeight:59], 2 / 414.0 * MAIN_WIDTH, 2);
        }else {
            _sectionBalanceLineView.frame = CGRectMake(MAIN_WIDTH - [CalculateFrameView setWidth:balanceLength + 23], [CalculateFrameView setHeight:59], balanceLength / 414.0 * MAIN_WIDTH, 2);
        }
        _sectionBalanceLabel.text = detailModal.sectionBalance;
    }
    return sectionView;
}

- (double)maxOfThreeNumberOne:(double)one two:(double)two three:(double)three {
    double a = fabs(one);
    double b = fabs(two);
    double c = fabs(three);
    
    return (a>b?(a>c?a:c):(b>c?b:c));
}


- (void)sectionViewTapAction:(UITapGestureRecognizer *)tap {
    if ([_selectedArray[[tap view].tag - 1000] isEqualToString:@"0"]) {
        
        //        for (NSInteger i = 0; i < _sectionArray.count; i++) {
        //            [_selectedArray replaceObjectAtIndex:i withObject:@"0"];
        //            [_tableView reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade];
        //        }
        
        
        //如果当前点击的section是缩回的,那么点击后就需要把它展开,是_selectedArray对应的值为1,这样当前section返回cell的个数就变为真实个数,然后刷新这个section就行了
        [_selectedArray replaceObjectAtIndex:[tap view].tag - 1000 withObject:@"1"];
        [_detailTableView reloadSections:[NSIndexSet indexSetWithIndex:[tap view].tag - 1000] withRowAnimation:UITableViewRowAnimationFade];
//        [_detailTableView reloadData];
    }
    else
    {
        //如果当前点击的section是展开的,那么点击后就需要把它缩回,使_selectedArray对应的值为0,这样当前section返回cell的个数变成0,然后刷新这个section就行了
        [_selectedArray replaceObjectAtIndex:[tap view].tag - 1000 withObject:@"0"];
        [_detailTableView reloadSections:[NSIndexSet indexSetWithIndex:[tap view].tag - 1000] withRowAnimation:UITableViewRowAnimationFade];
//        [_detailTableView reloadData];
    }
}


#pragma mark UITableView + 下拉刷新 自定义文字
- (void)headerRefresh
{
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    self.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadHeadData)];
    
//    [self setInitHeaderTitle];
    
    _header.lastUpdatedTimeLabel.hidden = YES;
    
    // 设置字体
    _header.stateLabel.font = [UIFont systemFontOfSize:15];
    _header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
    
    // 设置颜色
    _header.stateLabel.textColor = REFRESH_COLOR;
    
    // 设置刷新控件
    self.detailTableView.mj_header = _header;
}


#pragma mark 下拉刷新数据
- (void)loadHeadData
{
    
    [self setHeaderTitle];

    // 2.模拟2秒后刷新表格UI（真实开发中，可以移除这段gcd代码）
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(REFRESH_DURATION * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self createData];
        
        [self.detailTableView reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.detailTableView.mj_header endRefreshing];
    });
}

#pragma mark UITableView + 上拉刷新 自定义文字
- (void)footRefresh
{
    self.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadFootData)];
    
    
//    [self setInitFooterTitle];

    // 设置字体
    _footer.stateLabel.font = [UIFont systemFontOfSize:15];
    
    // 设置颜色
    _footer.stateLabel.textColor = REFRESH_COLOR;
    
    
    // 设置刷新控件
    self.detailTableView.mj_footer = _footer;
}

#pragma mark 上拉刷新数据
- (void)loadFootData
{
    [self setFooterTitle];
    
    // 2.模拟2秒后刷新表格UI（真实开发中，可以移除这段gcd代码）
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(REFRESH_DURATION * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self createData];
        // 刷新表格
        [self.detailTableView reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.detailTableView.mj_footer endRefreshing];
    });
}

- (void)setFooterTitle {

    NSInteger year = [[CalendarTool shareCalendarTool] year:_date];
    NSInteger oldMonth = [[CalendarTool shareCalendarTool] month:_date];
//    NSInteger day = [[CalendarTool shareCalendarTool] day:_date];
    
    NSInteger month = 0;
    if (oldMonth > 1) {
        month = oldMonth - 1;
    }else {
        month = 12;
        year -= 1;
    }

    // 设置文字
    [_footer setTitle:[NSString stringWithFormat:@"上拉切换%04ld年%02ld月流水", year, month] forState:MJRefreshStateIdle];
    [_footer setTitle:[NSString stringWithFormat:@"放开切换%04ld年%02ld月流水", year, month] forState:MJRefreshStatePulling];
    [_footer setTitle:[NSString stringWithFormat:@"正在载入%04ld年%02ld月流水", year, month] forState:MJRefreshStateRefreshing];
    
    _date = [[CalendarTool shareCalendarTool] dateFromString:[NSString stringWithFormat:@"%04ld-%02ld-01", year, month]];
    
    [self.delegate dateButtonTitleString:[NSString stringWithFormat:@"%04ld年%02ld月", year, month]];
    
    self.nowDate = _date;
}

- (void)setHeaderTitle {

    NSInteger year = [[CalendarTool shareCalendarTool] year:_date];
    NSInteger oldMonth = [[CalendarTool shareCalendarTool] month:_date];
//    NSInteger day = [[CalendarTool shareCalendarTool] day:_date];

    NSInteger month = 0;
    if (oldMonth < 12) {
        month = oldMonth + 1;
    }else {
        month = 1;
        year += 1;
    }
    
    // 设置文字
    [_header setTitle:[NSString stringWithFormat:@"下拉切换%04ld年%02ld月流水", year, month] forState:MJRefreshStateIdle];
    [_header setTitle:[NSString stringWithFormat:@"放开切换%04ld年%02ld月流水", year, month] forState:MJRefreshStatePulling];
    [_header setTitle:[NSString stringWithFormat:@"正在载入%04ld年%02ld月流水", year, month] forState:MJRefreshStateRefreshing];
    
    NSString *dateStr = [NSString stringWithFormat:@"%04ld-%02ld-01", year, month];
    _date = [[CalendarTool shareCalendarTool] dateFromString:dateStr];
    
    [self.delegate dateButtonTitleString:[NSString stringWithFormat:@"%04ld年%02ld月", year, month]];
    
    self.nowDate = _date;
}

- (void)setInitHeaderTitle {
    NSInteger year = [[CalendarTool shareCalendarTool] year:_nowDate];
    NSInteger month = [[CalendarTool shareCalendarTool] month:_nowDate];
    
    // 设置文字
    [_header setTitle:[NSString stringWithFormat:@"下拉切换%04ld年%02ld月流水", year, month] forState:MJRefreshStateIdle];
    [_header setTitle:[NSString stringWithFormat:@"放开切换%04ld年%02ld月流水", year, month] forState:MJRefreshStatePulling];
    [_header setTitle:[NSString stringWithFormat:@"正在载入%04ld年%02ld月流水", year, month] forState:MJRefreshStateRefreshing];
}

- (void)setInitFooterTitle {
    NSInteger year = [[CalendarTool shareCalendarTool] year:_nowDate];
    NSInteger month = [[CalendarTool shareCalendarTool] month:_nowDate];
    
    // 设置文字
    [_footer setTitle:[NSString stringWithFormat:@"上拉切换%04ld年%02ld月流水", year, month] forState:MJRefreshStateIdle];
    [_footer setTitle:[NSString stringWithFormat:@"放开切换%04ld年%02ld月流水", year, month] forState:MJRefreshStatePulling];
    [_footer setTitle:[NSString stringWithFormat:@"正在载入%04ld年%02ld月流水", year, month] forState:MJRefreshStateRefreshing];
}

@end
